"""
统一API响应模块
提供标准化的API响应格式，包含状态码、消息和数据
"""
from typing import Any, Dict, Optional, Union
from fastapi import status
from fastapi.responses import JSONResponse

from pkg.common.constants.response import ResponseCode, ResponseMessage


class ApiResponse:
    """
    统一API响应处理类
    """
    @staticmethod
    def success(data: Any = None, msg: str = ResponseMessage.SUCCESS, code: int = ResponseCode.SUCCESS) -> Dict:
        """
        成功响应
        :param data: 响应数据
        :param msg: 响应消息
        :param code: 响应状态码
        :return: 标准格式的响应字典
        """
        return {
            "code": code,
            "msg": msg,
            "data": data
        }
    
    @staticmethod
    def error(msg: str = ResponseMessage.SERVER_ERROR, code: int = ResponseCode.SERVER_ERROR, data: Any = None) -> Dict:
        """
        错误响应
        :param msg: 错误消息
        :param code: 错误状态码
        :param data: 错误相关数据
        :return: 标准格式的响应字典
        """
        return {
            "code": code,
            "msg": msg,
            "data": data
        }
    
    @staticmethod
    def json_response(
        data: Any = None,
        msg: str = ResponseMessage.SUCCESS,
        code: int = ResponseCode.SUCCESS,
        status_code: int = status.HTTP_200_OK,
        headers: Optional[Dict] = None
    ) -> JSONResponse:
        """
        返回JSON响应
        :param data: 响应数据
        :param msg: 响应消息
        :param code: 业务状态码
        :param status_code: HTTP状态码
        :param headers: 自定义头信息
        :return: FastAPI的JSONResponse对象
        """
        content = {
            "code": code,
            "msg": msg,
            "data": data
        }
        return JSONResponse(
            status_code=status_code,
            content=content,
            headers=headers
        )
    
    @staticmethod
    def paginate(
        items: list,
        total: int,
        page: int,
        page_size: int,
        msg: str = "分页数据获取成功"
    ) -> Dict:
        """
        分页数据响应
        :param items: 当前页的数据项列表
        :param total: 总记录数
        :param page: 当前页码
        :param page_size: 每页大小
        :param msg: 响应消息
        :return: 标准格式的分页响应，直接放置在顶层以符合Pydantic模型的验证
        """
        pages = (total + page_size - 1) // page_size if page_size > 0 else 0
        return {
            "items": items,
            "total": total,
            "page": page,
            "page_size": page_size,
            "pages": pages,
            "code": ResponseCode.SUCCESS,
            "msg": msg
        }


# 快捷方法，方便导入使用
def success(data: Any = None, msg: str = ResponseMessage.SUCCESS, code: int = ResponseCode.SUCCESS) -> Dict:
    """快捷方法：成功响应"""
    return ApiResponse.success(data, msg, code)


def error(msg: str = ResponseMessage.SERVER_ERROR, code: int = ResponseCode.SERVER_ERROR, data: Any = None) -> Dict:
    """快捷方法：错误响应"""
    return ApiResponse.error(msg, code, data)


def json_response(
    data: Any = None,
    msg: str = ResponseMessage.SUCCESS,
    code: int = ResponseCode.SUCCESS,
    status_code: int = status.HTTP_200_OK,
    headers: Optional[Dict] = None
) -> JSONResponse:
    """快捷方法：JSON响应"""
    return ApiResponse.json_response(data, msg, code, status_code, headers)


def paginate(
    items: list,
    total: int,
    page: int,
    page_size: int,
    msg: str = "分页数据获取成功"
) -> Dict:
    """快捷方法：分页响应"""
    return ApiResponse.paginate(items, total, page, page_size, msg)
